package com.burgeon.backend.EnterpriseWeChat.rest.Brand;

import com.burgeon.backend.EnterpriseWeChat.entity.Product;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindYuncangInventory extends RESTBase {
    private final static String RANGE = "10";//分页数量
    private String ip;
    private String account;
    private String password;

    public FindYuncangInventory(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Product> getData(String content, int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(content,page), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Product> productInventories= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                Product Product = new Product();
                Product.setProductValue(jsonArray.getJSONArray(i).getString(0));
                Product.setPriceList(jsonArray.getJSONArray(i).getString(1));
                Product.setInventory(jsonArray.getJSONArray(i).getString(2));
                Product.setProductId(jsonArray.getJSONArray(i).getString(3));
                Product.setProductName(jsonArray.getJSONArray(i).getString(4));
                Product.setBrandName(jsonArray.getJSONArray(i).getString(5));
               productInventories.add(Product);
            }
        }
        System.err.println(productInventories.size());
        return productInventories;
    }

    private MultiValueMap Data(String content,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'M_PRODUCT_ID;VALUE'");//品名
        colums.add("'PRICELIST'");//	吊牌价
        colums.add("QTYCAN");//	可用库存
        colums.add("M_PRODUCT_ID");//	商品
        colums.add("'M_PRODUCT_ID;NAME'");//图片款号
        colums.add("'M_DIM1_ID;ATTRIBNAME'");//商品的品牌名称

        params.put("columns", colums);
        params.put("table", "APP_STORAGEPRO_DIM");//表名
        params.put("count", "true");

        params.put("range",RANGE);
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        expr1.put("column","'M_PRODUCT_ID;NAME'");
        String a = "'"+content+"'";
        expr1.put("condition",a);

        expr2.put("column","'M_PRODUCT_ID;VALUE'");
        String b = "'"+content+"'";
        expr2.put("condition",b);

        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);
        paramss.put("combine","or");

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindYuncangInventory g = new FindYuncangInventory("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("ME2018",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
